package com.situ.student.view;

import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;

import com.situ.student.controller.StudentController;
import com.situ.student.entity.Student;
import com.situ.student.exception.IdNotFoundException;
import com.situ.student.exception.NameRepeatException;

public class StudentView {
	private Scanner scanner = new Scanner(System.in);
	private StudentController studentController = null;
	
	public StudentView() {
		studentController = new StudentController();
	}
	
	
	public void showView() {
		while (true) {
			System.out.println("----学生信息管理系统----");
			System.out.println("1.查询所有");
			System.out.println("2.添加");
			System.out.println("3.修改");
			System.out.println("4.删除");
			System.out.println("0.退出");
			System.out.println("请选择");
			int selected = scanner.nextInt();
			if (selected == 0) {
				System.out.println("退出程序");
				return;
			}
			switch (selected) {
			case 1:
				findAll();
				break;
			case 2://添加
				register();
				break;
			case 3:
				update();
				break;
			case 4:
				delete();
				break;

			default:
				break;
			}
		}
	}

	private void update() {
		System.out.println("请输入要修改信息的id");
		int id = scanner.nextInt();
		System.out.println("请输入要修改的姓名");
		String name = scanner.next();
		System.out.println("请输入要修改的年龄");
		int age = scanner.nextInt();
		System.out.println("请输入要修改的性别");
		String gender = scanner.next();
		boolean result = false;
		try {
			result = studentController.update(id,name,age,gender);
		} catch (IdNotFoundException e) {
			e.printStackTrace();
		}
		if (result) {
			System.out.println("修改成功");
		} else {
			System.out.println("修改失败");
		}
	}


	private void delete() {
		System.out.println("请输入要删除信息的id");
		int id = scanner.nextInt();
		boolean result = false;
		try {
			result = studentController.delete(id);
		} catch (IdNotFoundException e) {
			e.printStackTrace();
		}
		if (result) {
			System.out.println("删除成功");
		} else {
			System.out.println("删除失败");
		}
		
	}

	private void register() {
		while (true) {
			System.out.println("请输入用户名：");
			String name = scanner.next();
			System.out.println("请输入年龄：");
			int age = scanner.nextInt();
			System.out.println("请输入性别：");
			String gender = scanner.next();
			boolean isSuccess = false;
			try {
				isSuccess = studentController.register(name, age, gender);
			} catch (NameRepeatException e) {
				System.out.println(e.getMessage());
			}
			if (isSuccess) {
				System.out.println("注册成功");
			} else {
				System.out.println("注册失败");
			}
		}
	}
	
	private void findAll() {
		List<Student> list = studentController.finaAll();
		for (Student student : list) {
			System.out.println(student);
		}
	}
	
	

}
